package com.start_eternity.oasissafewalk.mapper;

import com.start_eternity.oasissafewalk.pojo.Route;
import com.start_eternity.oasissafewalk.pojo.RouteStation;
import com.start_eternity.oasissafewalk.pojo.Station;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface RouteMapper {

    //查询全部线路
    //@Select("select * from route")
    List<Route> list(String routeName, Short routeStatus, LocalDate begin, LocalDate end);

    //添加线路
    @Insert("insert into route" +
            "(route_name, route_des, start_point, end_point, create_time, update_time) " +
            "values" +
            "(#{routeName},#{routeDes},#{startPoint},#{endPoint},#{createTime},#{updateTime})")
    void insert(Route route);

    //删除线路
    @Delete("delete from route where route_id = #{routeId}")
    void deleteById(Integer id);

    //根据线路id获取线路Route对象
    @Select("select * from route where route_id = #{routeId}")
    Route getById(Integer id);

    //修改
    void update(Route route);

    //查询线路中的所有站点(多变查询)
    @Select("select * from route where route_id = #{routeId}")
    @Results({
            /*
            @Result(id = true,property = "routeId",column = "route_id"),
            @Result(property = "routeName",column = "route_name"),
            @Result(property = "routeDes",column = "route_des"),
            @Result(property = "routeStatus",column = "route_status"),
            @Result(property = "routeCapacity",column = "route_capacity"),
            @Result(property = "addDate",column = "add_date"),
            @Result(property = "startPoint",column = "start_point"),
            @Result(property = "endPoint",column = "end_point"),
            @Result(property = "startTime",column = "start_time"),
            @Result(property = "endTime",column = "end_time"),
            @Result(property = "createTime",column = "create_time"),
            @Result(property = "updateTime",column = "update_time"),
            */
            @Result(property = "routeStationList",column = "route_id",
                    javaType = List.class,
                    many = @Many(select = "getByRouteStationId"))
    })
    Route getRouteWithStations(Integer routeId);

    @Select("select * from route_station where route_id = #{routeId}")
    @Results({
            /*
            @Result(id = true,property = "routeStationId",column = "route_station_id"),
            @Result(property = "routeId",column = "route_id"),
            @Result(property = "stationId",column = "station_id"),
            @Result(property = "stationOrder",column = "station_order"),
            @Result(property = "travelTime",column = "travel_time"),
            */
            @Result(property = "station",column = "station_id",
            javaType = Station.class,
            one = @One(select = "com.start_eternity.oasissafewalk.mapper.StationMapper.getById"))
    })
    List<RouteStation> getByRouteStationId(Integer routeId);

    //新增线路站点
    @Insert("insert into route_station " +
            "(route_id, station_id, station_order, travel_time) " +
            "values" +
            "(#{routeId},#{stationId},#{stationOrder},#{travelTime})")
    void insertRouteStation(RouteStation routeStation);

    //删除线路站点
    @Delete("delete from route_station where route_station_id = #{routeStationId}")
    void deleteByRouteStationId(Integer routeStationId);

    //修改线路站点
    @Select("select * from route_station where route_station_id = #{routeStationId}")
    RouteStation getOneByRouteStationId(Integer routeStationId);

    void updateRouteStation(RouteStation routeStation);
}
